/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.tpj.database.zamestnanec;

import cz.tpj.database.DatabaseConnector;
import cz.tpj.util.ErrorDatabase;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


/**
 *
 * @author jabo
 */
public class Table_zamestnanec {
    public static final String ID = "id";
    public static final String LOGIN = "login";
    public static final String PASS = "pass";
    public static final String NAME = "meno";
    public static final String SURNAME = "priezvisko";
    public static final String TABLE = "zamestnanec";

    private static Table_zamestnanec im = null;
    private Connection con;
    private Statement stmt = null;

    /**
     * Vrací jedinou instanci této třídy.
     * @return Table_zamestnanec
     */
    
    public static Table_zamestnanec getInstance(){
        if(im == null){
            im = new Table_zamestnanec();
        }
        return im;
    }
    //=======================metody-------------------------
    private void databaseConnect() throws SQLException{
        DatabaseConnector dc = DatabaseConnector.getInstance();
        con = dc.getConnection();
        stmt = con.createStatement();
    }
    //--------------------------------------------------------------------------
    
    /**
     * Nahrá zamestnanca na základe zadaného mena a hesla.
     * @return Zamestnanec
     */

    public Zamestnanec getEmployeeByLoginAndPass(String login, String pass) throws ErrorDatabase{
        ResultSet rs = null;
        Zamestnanec score = null;
        try {
            databaseConnect();
            rs = stmt.executeQuery("SELECT * FROM "+TABLE+" WHERE "+LOGIN+" = '"+login+"' AND "+PASS+" = '"+pass+"';");

            Zamestnanec z;

            String r_meno, r_priezvisko, r_login, r_pass, id;
            if(rs.next()){
                id = rs.getString(ID);
                r_meno = rs.getString(NAME);
                r_priezvisko = rs.getString(SURNAME);
                r_login = rs.getString(LOGIN);
                r_pass = rs.getString(PASS);
                score = new Zamestnanec(id, r_meno, r_priezvisko, r_login, r_pass);
            }
        } catch (ErrorDatabase ex) {
            throw new ErrorDatabase(ErrorDatabase.INVALID_DATA_FORMAT, "V databzí byla nalezena nekonzistentvní data.");
        } catch (SQLException ex) {
            throw new ErrorDatabase(ErrorDatabase.ERROR_DATABASE_CONECTION, "Chyba při načítání dat z databáze.");
        } finally{
            try {
                rs.close();
                stmt.close();
            } catch (Exception ex) {
            }
        }
        return score;
    }


}
